package algorithm.MaxCommonSubstring;

public class MaxCommonSubstring {
    public static int getMax(String str1, String str2) {
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();
        int max = 0;
        for (int i = str1.length()-1; i >= 0; i--) {
            for (int j = str2.length()-1; j >= 0; j--) {
                if (s1[i] == s2[j]) {
                    int max1 = process(s1, s2, i - 1, j - 1);
                    max1++;
                    max = Math.max(max, max1);
                }
            }
        }
        return max;
    }

    public static int process(char[] s1, char[] s2, int y, int x) {
        if (y > 0 && x > 0 && s1[y] == s2[x]) {
            int max1 = process(s1, s2, y - 1, x - 1);
            max1++;
            return max1;
        } else {
            return 0;
        }

    }

    public static void main(String[] args) {
        String str1 = "a12b123c";
        String str2 = "a1b123bc";
        int res = getMax(str1, str2);
        System.out.println(res);
    }
}
